﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Ccit.Utils;
using Newtonsoft.Json;
using System.Collections.Generic;
using Microsoft.Web.UI.WebControls;


namespace Ccit.OA.Email
{
    public partial class email_wdyj_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                initializePage();
            }
        }


        private void initializePage()
        {
            createTree();
            trvSign.ExpandLevel = 1;
        }

        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            string sOrganName = "个人签名";

            oRoot.Text = sOrganName;
            oRoot.ImageUrl = "../images/toolbar/Folder_open.gif";
            oRoot.NavigateUrl = "";
            oRoot.Target = "";
            oRoot.ID = "0_0";
        }


        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createRootNode(ref oRoot);
            trvSign.Nodes.Add(oRoot);
            BindTree(oRoot);
        }

        /// <summary>
        /// 绑定树
        /// </summary>
        private void BindTree(Microsoft.Web.UI.WebControls.TreeNode node)
        {
            EmailBLL bll = new EmailBLL();
            LoginUser loginUser = Session["LogUserInfo"] as LoginUser;
            MsEmailSigna msSigna = new MsEmailSigna();
            msSigna.userId = loginUser.UserId;
            DataSet dSet = bll.GetSigna(msSigna);
            DataView dv = new DataView(dSet.Tables[0]);
            AddTreeNodes(dv, node);//为树添加节点

            bll.Release();
            bll = null;
            msSigna = null;
        }

        /// <summary>
        /// 为树添加节点
        /// </summary>
        /// <param name="dv"></param>
        /// <param name="node"></param>
        private void AddTreeNodes(DataView dv, Microsoft.Web.UI.WebControls.TreeNode node)
        {
            foreach (DataRowView dr in dv)
            {
                Microsoft.Web.UI.WebControls.TreeNode td = new Microsoft.Web.UI.WebControls.TreeNode();
                string sParam = "Id=" + dr["EML_A5_10"].ToString();
                string encodeparam = Base64Lib.Encode(sParam);
                
                td.Text = dr["EML_A5_30"].ToString();
                td.ImageUrl = "../images/toolbar/filelogo.gif";
                td.NavigateUrl = "email_wdyj_edit.aspx?" + encodeparam;
                td.ExpandedImageUrl = "../images/toolbar/del_bq.gif";
                td.Target = "iFrmEdit";
                //td.Type = "root";
                td.ID = dr["EML_A5_10"].ToString();

                node.Nodes.Add(td);
            }
        }


        /// <summary>
        /// 删除ajax方法 邮件夹下边所有邮件
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteSign(string serExpr)
        {
            bool bFlag = false;
            MsEmailSigna msSigna = (MsEmailSigna)JavaScriptConvert.DeserializeObject(serExpr, typeof(MsEmailSigna));
            EmailBLL bll = new EmailBLL();
            bFlag = bll.DelSign(msSigna);
            string sResult = (bFlag) ? "1" : "0";
            var newObj = new {resultSign = sResult ,resultSignId = msSigna.Id};
            string resultSerExpr = JavaScriptConvert.SerializeObject(newObj);

            bll.Release();
            bll = null;
            newObj = null;
            msSigna = null;
            return resultSerExpr;

        }
    }
}
